package com.yunhui.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunhui.entity.Comment;
import com.yunhui.entity.Merchant;
import com.yunhui.entity.RouteJb;
import com.yunhui.mapper.RouteJbMapper;
import com.yunhui.service.CommentService;
import com.yunhui.service.RouteJbService;
import com.yunhui.util.PageSupport;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  评论的前端控制器
 * </p>
 *
 * @author WGH
 * @since 2019-12-15
 */
@Controller
@RequestMapping("/comment")
public class CommentController {


    /**
     * 注入业务类
     */
    @Resource
   private CommentService commentService;

    /**
     * 行程的业务类
     */
    @Resource
    private RouteJbService routeJbService;


    /**
     * 进入后台评论页面
     * @return
     */
    @RequestMapping("golist")
    public String goCommentList(String index, Model model,HttpSession session){
        //通过session获取商家id\
        //可根据商家id查询
        //String mid = session.getAttribute("mId").toString();
        /**
         * 当前页
         */
        int index1=1;
        //判断当前页是否为空
        if(index!=null && !"".equals(index)){
            index1 = Integer.parseInt(index);
        }
        /**
         * 获取总条数
         */
        int count = commentService.selectCount(0,0);
        //获取分页集合
        List<Comment> list = commentService.finAllPage(index1,10,0,0);

        //创建分页对象
        PageSupport page = new PageSupport();
        // 当前页码
        page.setCurrentPageNo(index1);
        //每一页显示的数量
        page.setPageSize(10);
        //总条数
        page.setTotalCount(count);

        //数据回显
        model.addAttribute("commList",list);
        model.addAttribute("page",page);

        return "houtai/comment-list1";
    }
    /**
     * 进入商家评论页面
     * @return
     */
    @RequestMapping("goshanglist")
    public String goShangtList(HttpSession session,String index, Model model,String rid){
        //通过session获取商家id
       // String mId = session.getAttribute("shang").toString();
       Merchant m = (Merchant) session.getAttribute("merchant");
       List<RouteJb> rList = routeJbService.list(new QueryWrapper<RouteJb>().eq("mId",m.getId()));
        model.addAttribute("rlist",rList);
        int ridInt =0;
        if(rid!=null && !"".equals(rid)&&!"null".equals(rid)){
            ridInt = Integer.parseInt(rid);
        }

        /**
         * 当前页
         */
        int index1=1;
        if(index!=null && !"".equals(index)){
            index1 = Integer.parseInt(index);
        }
        /**
         * 获取总条数
         */
        int count = commentService.selectCount(m.getId(),ridInt);
        //获取分页集合
        List<Comment> list = commentService.finAllPage(index1,10,m.getId(),ridInt);

        //创建分页对象
        PageSupport page = new PageSupport();
        // 当前页码
        page.setCurrentPageNo(index1);
        page.setPageSize(10);
        page.setTotalCount(count);


        model.addAttribute("commList",list);
        model.addAttribute("page",page);
        model.addAttribute("rid",ridInt);

        return "shang/comment-list1";
    }

    /**
     * 商家评论回复功能
     * @return
     */
    @RequestMapping("reply")
    @ResponseBody
    public Object updataComment(Comment comment){
        int num = 0;
        comment.setReplyTime(new Date());
        num = commentService.updataComment(comment);

        if(num>0){
            return "1";
        }
        return "0";
    }

    /**
     * 跳转用户评论列表
     * @param session 通过session获取用户id
     * @param context  标识查看用户查询已回复或者未回复
     * @return
     */
    @RequestMapping("gomycomment")
    public  String goMy_comment(HttpSession session,String context,String index,Model model){
        //通过session获取用户id
       // String id = session.getAttribute("UserId").toString();
        int uid = 1;


        /**
         * 当前页
         */
        int index1=1;
        if(index!=null && !"".equals(index)){
            index1 = Integer.parseInt(index);
        }
        if("null".equals(context)){
            context = null;
        }
        int count = commentService.selectCountByUser(uid,context);
        //获取分页集合
        List<Comment> list = commentService.selectByuId(index1,5,uid,context);

        //创建分页对象
        PageSupport page = new PageSupport();
        // 当前页码
        page.setCurrentPageNo(index1);
        page.setPageSize(10);
        page.setTotalCount(count);

        model.addAttribute("commList",list);
        model.addAttribute("page",page);
        model.addAttribute("context",context);

        return "my_comment";
    }

    /**
     * 用户添加评论
     */
    @RequestMapping("addComment")
    @ResponseBody
    public Object addComment(Comment comment){
        commentService.add(comment);
        return "";
    }

    /**
     * 用户删除评论
     * @param cId 通过id删除
     * @return
     */
    @RequestMapping("deleteComment")
    @ResponseBody
    public Object deleteComment(String cId){
        if(cId==null||"".equals(cId)){
            return "-1";
        }
        int id = Integer.parseInt(cId);

        if(commentService.deleteComment(id)){
            return "1";
        }
        return "-1";
    }
    @RequestMapping("goxiang")
    public String goXiangQing(){
        return "xiangqing";
    }

    /**
     * 详情页的评论分页，根据行程的id查询分页集合 ajax请求
     * @param index 当前页
     * @param jId   行程id
     * @return
     */
    @RequestMapping("xiangComment")
    @ResponseBody
    public Object getListComment(String index,String jId){
        int index1 = Integer.parseInt(index);
        List<Comment> list = commentService.selectByJid(index1,5,"7");

        return list;
    }

    /**
     * 使用ajax加载评论分页的总页数
     * @param jId
     * @return
     */
    @RequestMapping("xiangPage")
    @ResponseBody
    public Object getXiangQin(String jId){
       int num = commentService.selectCountByJid("7");
        //创建分页对象
        PageSupport page = new PageSupport();
        // 当前页码
        page.setCurrentPageNo(1);
        page.setPageSize(10);
        page.setTotalCount(num);
       return page.getTotalPageCount();
    }

    @Resource
    private  RouteJbMapper routeJbMapper;

    @RequestMapping("getJibenById")
    @ResponseBody
    public Object getJiById(String Id){

        return  routeJbMapper.selectById(Integer.parseInt(Id));
    }


    @RequestMapping("getJibenByTitle")
    @ResponseBody
    public Object getJiByTitle(String title,String destination){

        return  routeJbMapper.selectList(new QueryWrapper<RouteJb>().like("title", title).like("destination", destination));
    }
    @RequestMapping("golohun")
    public String golohun(){
        return "localjoin";
    }

    @RequestMapping("gopack")
    public String golohu2(){
        return "pack";
    }
}

